// ========================
// RSDK Project Sonic CD
// Script Name: R4.5 Setup
// Script Author: Twanvanb1
// ========================

// ========================
// Aliases
// ========================

#alias Object.Value0	:	Object.Belt
#alias Object.Value1	:	Object.PaletteTimer
#alias Object.Value2	:	Object.BeltDelay
#alias Object.Value3	:	Object.RotatePlatDetect
#alias Object.Value4	:	Object.RotatePlatFrame
#alias Object.Value5	:	Object.Timer
#alias Object.Value6	:	Object.AniTileTimer
#alias Object.Value7	:	Object.AniTileIndex

// Ink Effect Aliases
#alias 4 : INK_SUB    // Uses transparency value of Object.Alpha, and a dark tint

// HUD Alias
#alias Object[24].PropertyValue	:	HUD.CurrentTimePeriod

// Time Periods
#alias 1	:	TIME_PAST

// Gravity
#alias 1	:	GRAVITY_AIR

// Stage SFX
#alias 1	:	SFX_S_BUMPER2
#alias 6	:	SFX_S_MINIBUMPER

// Priority
#alias 1	:	PRIORITY_ACTIVE

// Tile Info
#alias 1	:	TILEINFO_DIRECTION
#alias 6	:	TILEINFO_ANGLEA

// Deformation Layer
#alias 1	:	DEFORM_FG_WATER
#alias 2	:	DEFORM_BG

#alias 0	:	TimePeriod_Present
#alias 1	:	TimePeriod_Past
#alias 2	:	TimePeriod_GoodF
#alias 3	:	TimePeriod_BadF

#alias 26	:	SLOT_BGEFFECTS


// ========================
// Function Declarations
// ========================

#function FGEffects_Deform
#function BGEffects_Deform


// ========================
// Function Definitions
// ========================

function FGEffects_Deform
	Stage[ArrayPos0].DeformationData0 = 0
	ArrayPos0++
	Stage[ArrayPos0].DeformationData0 = 0
	ArrayPos0++
	Stage[ArrayPos0].DeformationData0 = 1
	ArrayPos0++
	Stage[ArrayPos0].DeformationData0 = 1
	ArrayPos0++
	Stage[ArrayPos0].DeformationData0 = 1
	ArrayPos0++
	Stage[ArrayPos0].DeformationData0 = 0
	ArrayPos0++
	Stage[ArrayPos0].DeformationData0 = 0
	ArrayPos0++
	Stage[ArrayPos0].DeformationData0 = 0
	ArrayPos0++
	Stage[ArrayPos0].DeformationData0 = 0
	ArrayPos0++
	Stage[ArrayPos0].DeformationData0 = 1
	ArrayPos0++
	Stage[ArrayPos0].DeformationData0 = 0
	ArrayPos0++
	Stage[ArrayPos0].DeformationData0 = 0
	ArrayPos0++
	Stage[ArrayPos0].DeformationData0 = 0
	ArrayPos0++
	Stage[ArrayPos0].DeformationData0 = 0
	ArrayPos0++
	Stage[ArrayPos0].DeformationData0 = 1
	ArrayPos0++
	Stage[ArrayPos0].DeformationData0 = 0
	ArrayPos0++
	Stage[ArrayPos0].DeformationData0 = 0
	ArrayPos0++
	Stage[ArrayPos0].DeformationData0 = 0
	ArrayPos0++
	Stage[ArrayPos0].DeformationData0 = 0
	ArrayPos0++
	Stage[ArrayPos0].DeformationData0 = 0
	ArrayPos0++
	Stage[ArrayPos0].DeformationData0 = 0
	ArrayPos0++
	Stage[ArrayPos0].DeformationData0 = 0
	ArrayPos0++
	Stage[ArrayPos0].DeformationData0 = 0
	ArrayPos0++
	Stage[ArrayPos0].DeformationData0 = 0
	ArrayPos0++
	Stage[ArrayPos0].DeformationData0 = 0
	ArrayPos0++
	Stage[ArrayPos0].DeformationData0 = 0
	ArrayPos0++
	Stage[ArrayPos0].DeformationData0 = 1
	ArrayPos0++
	Stage[ArrayPos0].DeformationData0 = 0
	ArrayPos0++
	Stage[ArrayPos0].DeformationData0 = 0
	ArrayPos0++
	Stage[ArrayPos0].DeformationData0 = 1
	ArrayPos0++
	Stage[ArrayPos0].DeformationData0 = 1
	ArrayPos0++
	Stage[ArrayPos0].DeformationData0 = 0
	ArrayPos0++
	Stage[ArrayPos0].DeformationData0 = 0
	ArrayPos0++
end function

function BGEffects_Deform
	Stage[ArrayPos0].DeformationData2 = -2
	ArrayPos0++
    Stage[ArrayPos0].DeformationData2 = 1
	ArrayPos0++
    Stage[ArrayPos0].DeformationData2 = 2
	ArrayPos0++
    Stage[ArrayPos0].DeformationData2 = 2
	ArrayPos0++
    Stage[ArrayPos0].DeformationData2 = -1
	ArrayPos0++
    Stage[ArrayPos0].DeformationData2 = 2
	ArrayPos0++
    Stage[ArrayPos0].DeformationData2 = 2
	ArrayPos0++
    Stage[ArrayPos0].DeformationData2 = 1
	ArrayPos0++
    Stage[ArrayPos0].DeformationData2 = 2
	ArrayPos0++
    Stage[ArrayPos0].DeformationData2 = -1
	ArrayPos0++
    Stage[ArrayPos0].DeformationData2 = -2
	ArrayPos0++
    Stage[ArrayPos0].DeformationData2 = -2
	ArrayPos0++
    Stage[ArrayPos0].DeformationData2 = -2
	ArrayPos0++
    Stage[ArrayPos0].DeformationData2 = 1
	ArrayPos0++
    Stage[ArrayPos0].DeformationData2 = -1
	ArrayPos0++
    Stage[ArrayPos0].DeformationData2 = -1
	ArrayPos0++
    Stage[ArrayPos0].DeformationData2 = -1
	ArrayPos0++
    Stage[ArrayPos0].DeformationData2 = 0
	ArrayPos0++
    Stage[ArrayPos0].DeformationData2 = -2
	ArrayPos0++
    Stage[ArrayPos0].DeformationData2 = 0
	ArrayPos0++
    Stage[ArrayPos0].DeformationData2 = 0
	ArrayPos0++
    Stage[ArrayPos0].DeformationData2 = 0
	ArrayPos0++
    Stage[ArrayPos0].DeformationData2 = -2
	ArrayPos0++
    Stage[ArrayPos0].DeformationData2 = 0
	ArrayPos0++
    Stage[ArrayPos0].DeformationData2 = -2
	ArrayPos0++
    Stage[ArrayPos0].DeformationData2 = 2
	ArrayPos0++
    Stage[ArrayPos0].DeformationData2 = 0
	ArrayPos0++
    Stage[ArrayPos0].DeformationData2 = -2
	ArrayPos0++
    Stage[ArrayPos0].DeformationData2 = 2
	ArrayPos0++
    Stage[ArrayPos0].DeformationData2 = 2
	ArrayPos0++
    Stage[ArrayPos0].DeformationData2 = -1
	ArrayPos0++
    Stage[ArrayPos0].DeformationData2 = -2
	ArrayPos0++
end function


// ========================
// Subs
// ========================

sub ObjectMain
	//Object.BeltDelay connects platform and belt
	Object.PaletteTimer++
	switch Object.BeltDelay
		case 0
			if Object.PaletteTimer == 2
				Object.PaletteTimer = 0
				if Object.Belt == 0
					RotatePalette(171, 174, 0)
				else
					RotatePalette(171, 174, 1)
				end if
				Object.BeltDelay++
			end if
			break
		case 1
			if Object.PaletteTimer == 2
				Object.PaletteTimer = 0
				if Object.Belt == 0
					RotatePalette(171, 174, 0)
				else
					RotatePalette(171, 174, 1)
				end if
				Object.BeltDelay++
			end if
			break
		case 2
			if Object.PaletteTimer == 2
				Object.PaletteTimer = 0
				if Object.Belt == 0
					RotatePalette(171, 174, 0)
				else
					RotatePalette(171, 174, 1)
				end if
				Object.BeltDelay++
			end if
			break
		case 3
			if Object.PaletteTimer == 2
				Object.PaletteTimer = 0
				if Object.Belt == 0
					RotatePalette(171, 174, 0)
				else
					RotatePalette(171, 174, 1)
				end if
				Object.BeltDelay = 0
			end if
			break
	end switch
	
	if HUD.CurrentTimePeriod == 0
		if Object.RotatePlatDetect == 1
			if Object.RotatePlatFrame == 8
				Object.RotatePlatFrame = 0
				Object.RotatePlatDetect = 0
			else
				Object.Frame++
				if Object.Frame == 3
					Object.Frame = 0
					Object.RotatePlatFrame++
				end if
			end if
		end if
	end if

	Object.AnimationTimer++
	if Object.AnimationTimer == 10
		Object.AnimationTimer = 0
		Object.Frame++
		Object.Frame %= 6

		TempValue0 = 258
		TempValue0 += Object.Frame
		TempValue1 = 255
		while TempValue1 <= 257
			Copy16x16Tile(TempValue1, TempValue0)
			TempValue0 += 6
			TempValue1++
		loop

		TempValue0 = 279
		TempValue0 += Object.Frame
		TempValue1 = 276
		while TempValue1 <= 278
			Copy16x16Tile(TempValue1, TempValue0)
			TempValue0 += 6
			TempValue1++
		loop
	end if

	if Object.AniTileTimer == 0
		Object.AniTileIndex++
		Object.AniTileIndex %= 4

		TempValue0 = 385
		TempValue0 += Object.AniTileIndex
		Copy16x16Tile(333, TempValue0)

		switch Object.AniTileIndex
		case 0
		case 2
			Object.AniTileTimer = 32
			break
		case 1
		case 3
			Object.AniTileTimer = 4
			break
		end switch
	else
		Object.AniTileTimer--
	end if
end sub


sub ObjectDraw
#platform: SW_Rendering
	SetLayerDeformation(1, 32, 3, 1, 0, 16)
	SetLayerDeformation(1, 32, 3, 1, 16, 16)
	SetLayerDeformation(1, 32, -3, 1, 48, 16)
	SetLayerDeformation(1, 32, 3, 1, 96, 16)
	SetLayerDeformation(1, 32, -3, 1, 128, 16)
#endplatform

	Object.Timer++
	if Object.Timer > 1
		TileLayer[0].DeformationOffsetW++
		TileLayer[1].DeformationOffsetW++
	end if

	if Object.Timer == 4
		TileLayer[0].DeformationOffset++
	end if
	
	if Object.Timer == 6
		TileLayer[1].DeformationOffset++
		Object.Timer = 0
	end if

	TempValue0  = Stage.WaterLevel
	TempValue0 -= Screen.YOffset

	if TempValue0 < 0
		TempValue0 = 0
	end if

	if TempValue0 > Screen.YSize
		TempValue0 = Screen.YSize
	end if

	// Yes, we are using a sprite to draw the water
	// It looks nicer and is a type of palette blending.
	TempValue1 = 0
    TempValue2 = 0
    while TempValue2 <= screen.ysize
		TempValue3 = TempValue0
		TempValue3 += TempValue2
		while TempValue1 <= screen.xsize
			DrawSpriteScreenFX(0, FX_INK, TempValue1, TempValue3)
			TempValue1 += 16
		loop
		TempValue1 = 0
		TempValue2 += 16
    loop
end sub


sub ObjectStartup
	Object[19].Type = TypeName[R4.5 Setup]
	Object[19].Priority = PRIORITY_ACTIVE

	Object[19].DrawOrder = 6
	object[19].alpha = 0x50
	object[19].inkEffect = INK_SUB

	LoadSpriteSheet("R4.5/Water.gif")
	//SpriteFrame(0, 0, 1, 1, 0, 255)
	SpriteFrame(0, 0, 16, 16, 165, 69) 



#platform: SW_Rendering
	ArrayPos0 = 0
	CallFunction(FGEffects_Deform)

	while ArrayPos0 < 576
		ArrayPos1  = ArrayPos0
		ArrayPos1 -= 32
		Stage[ArrayPos0].DeformationData0 = Stage[ArrayPos1].DeformationData0
		ArrayPos0++
	loop

	ArrayPos0 = 0
	CallFunction(BGEffects_Deform)
	while ArrayPos0 < 576
		ArrayPos1  = ArrayPos0
		ArrayPos1 -= 32
		Stage[ArrayPos0].DeformationData2 = Stage[ArrayPos1].DeformationData2
		ArrayPos0++
	loop
#endplatform

	SetLayerDeformation(3, 128, 6, 0, 0, 0)
end sub

// ========================
// Editor Subs
// ========================

sub RSDKDraw
	DrawSprite(0)
end sub


sub RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)		// #0 - "Script" Icon
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end sub
